// pages/storeOrderInfo/storeOrderInfo.js
const app = getApp();
import api from '../../../utils/util.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    fullHeight: app.globalData.fullcontain,
    discount: app.globalData.discount,
    navmess:{
      navtitle: "填写订单信息",
      backhide: 0,
      topheight: 1
    },
  },
  pageto: function(e){
    var url = e.currentTarget.dataset.url,
        data = e.currentTarget.dataset.datobj;
    app.goto(url,data);
  },
  bindMinus: function (e) {
    var index = parseInt(e.currentTarget.dataset.index),
        quantity = this.data.demolist[index].quantity,
        price = this.data.demolist[index].price,
        id = e.currentTarget.dataset.id;
    var demolist = this.data.demolist;
    if(quantity>0){
      quantity--;
      this.setData({
        count: this.data.count - 1 * price,
        number: this.data.number - 1
      });
      var params = {
        "token": this.data.token,
        "id": id,
        "quantity": quantity
      }
      api.post("/mini/my/cart/modifyCart",params).then(res =>{
        if (res.code == 200) {
            
        }
      }).catch(res => {
        console.log("请求失败：",res);
        return false;
      })
    }
    demolist[index].quantity = quantity;
    this.setData({
      demolist: demolist
    });
  },
  bindPlus: function (e) {
    var that = this,
        index = parseInt(e.currentTarget.dataset.index),
        quantity = this.data.demolist[index].quantity,
        price = this.data.demolist[index].price,
        id = e.currentTarget.dataset.id;
    var demolist = this.data.demolist;
    quantity++;
    demolist[index].quantity = quantity;
    this.setData({
      count: this.data.count + 1 * price,
      number: this.data.number + 1,
      demolist: demolist
    });
    var params = {
      "token": this.data.token,
      "id": id,
      "quantity": quantity
    }
    api.post("/mini/my/cart/modifyCart",params).then(res =>{
      if (res.code == 200) {
          
      }
    }).catch(res => {
      console.log("请求失败：",res);
      return false;
    })
  },
  // 监听编辑信息
  gettextarea: function(e){
    this.setData({
      remark: e.detail.value
    })
  },
  usernametxt: function(e){
    this.setData({
      username: e.detail.value
    })
  },
  phonetxt: function(e){
    this.setData({
      phone: e.detail.value
    })
  },
  // 购物车下单
  toObligation: function(e){
    var that =this,
        url = e.currentTarget.dataset.url,
        ent = app.globalData.ent,
        receiveSmallArea = this.data.receiveSmallArea,
        receiveAddress = this.data.receiveAddress,
        userName = this.data.userName,
        phone = this.data.phone,
        remark = this.data.remark;
    var demolist = that.data.demolist, //取商品json
        goods = []; 
    for(var i = 0;i < demolist.length;i++){
      goods[i] = {
        id: demolist[i].id,
        goodsType: demolist[i].goodsType
      }
    }
    var message = {
      goods: goods,
      ent: ent,
      receiveSmallArea: receiveSmallArea,
      receiveAddress: receiveAddress,
      userName: userName,
      phone: phone,
      remark: remark
    }
    wx.navigateTo({
      url: url + "?message=" + JSON.stringify(message),
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 接收购物车选择商品
    console.log("ids: ",JSON.parse(options.selectIds));
    var token = wx.getStorageSync('token');
    this.setData({
      selectIds: JSON.parse(options.selectIds),
      token: token
    })
    api.post("/mini/my/pay/cartSettlement",{
      "token": that.data.token,
      "ids": that.data.selectIds
    }).then(res => {
      console.log(res)
      var demolist = res.result.cartList,
          count=0,number=0;
      for(var i=0;i < demolist.length;i++){
        count += Number(demolist[i].price) * Number(demolist[i].quantity);
        number += Number(demolist[i].quantity);
      }
      that.setData({
        demolist: demolist,
        count: count,
        number: number,
      })
    }).catch(res => {
      console.log("请求失败：",res);
      return false;
    })
    // 取默认收货信息
    api.post("/mini/my/basic/logistics",{
      "token": token
    }).then(res => {
      console.log("收货地址列表",res)
      for(var i=0;i<res.result.length;i++){
        if(res.result[i].defaulted){
          that.setData({
            userName: res.result[i].receiver,
            phone: res.result[i].receiveMobile,
            receiveSmallArea: res.result[i].id,
            receiveAddress: res.result[i].receiveAddress,
            adress: res.result[i].receiveAddress.join(' ')  //详细地址数组
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})